import axios from '@/libs/api.request'
import config from '@/config'
import {
    getToken
} from '@/libs/util'


export const getCascade = () => {
    return axios.ajax_post('admin-base-ajax_get_cp_linkage', {})
}

export const getDepartment = () => {
    return axios.ajax_post('admin-department-ajax_get_public_department', {})
}

/**
 * 导出Excel
 * @param {String} url 请求地址 
 * @param {Object} data 请求数据 {解构赋值，防止形参改变实参}
 * @param {Boolean} isPaging 是否分页
 */
export const exportExcel = (url, {
    ...data
}, isPaging = true) => {

    // 实际请求地址
    let itemUrl = `${config.conf.download_prefix + url}?`

    // 获取token
    data.token = getToken()

    // 判断是否分页
    if (isPaging) {
        data.page = 0
        data.rows = 0
    }

    // 编辑请求数据
    for (const key in data) {
        itemUrl += `${key}=${data[key]}&`
    }
    itemUrl = itemUrl.slice(0, -1)

    // 页面跳转
    location.href = itemUrl
}

export const getTableData = () => {
    return axios.request({
        url: 'get_table_data',
        method: 'get'
    })
}

export const getDragList = () => {
    return axios.request({
        url: 'get_drag_list',
        method: 'get'
    })
}

export const errorReq = () => {
    return axios.request({
        url: 'error_url',
        method: 'post'
    })
}

export const saveErrorLogger = info => {
    return axios.request({
        url: 'save_error_logger',
        data: info,
        method: 'post'
    })
}

export const uploadImg = formData => {
    return axios.request({
        url: 'image/upload',
        data: formData
    })
}

export const getOrgData = () => {
    return axios.request({
        url: 'get_org_data',
        method: 'get'
    })
}

export const getTreeSelectData = () => {
    return axios.request({
        url: 'get_tree_select_data',
        method: 'get'
    })
}